package com.example.hrsystem.repository;

import android.app.Application;
import android.content.Context;

import androidx.lifecycle.LiveData;

import com.example.hrsystem.data.source.AppDatabase;
import com.example.hrsystem.model.DailyAttendanceRecord;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 日常考勤数据仓库
 * 负责处理日常考勤记录的数据操作
 */
public class DailyAttendanceRepository {
    private AppDatabase database;
    private ExecutorService executor;

    public DailyAttendanceRepository(Context context) {
        database = AppDatabase.getDatabase(context);
        executor = Executors.newFixedThreadPool(4);
    }

    public LiveData<List<DailyAttendanceRecord>> getAllDailyAttendanceRecords() {
        return database.dailyAttendanceDao().getAllDailyAttendanceRecords();
    }

    /**
     * 获取特定员工的所有打卡记录
     */
    public LiveData<List<DailyAttendanceRecord>> getAttendanceRecordsByEmployeeId(String employeeId) {
        return database.dailyAttendanceDao().getDailyAttendanceRecordsByDateRange(employeeId, new Date(0), new Date());
    }

    /**
     * 插入打卡记录
     */
    public void insert(DailyAttendanceRecord record) {
        executor.execute(() -> database.dailyAttendanceDao().insert(record));
    }

    /**
     * 更新打卡记录
     */
    public void update(DailyAttendanceRecord record) {
        executor.execute(() -> database.dailyAttendanceDao().update(record));
    }

    /**
     * 删除打卡记录
     */
    public void delete(DailyAttendanceRecord record) {
        executor.execute(() -> database.dailyAttendanceDao().deleteById(record.getId()));
    }

    public void deleteById(int id) {
        executor.execute(() -> database.dailyAttendanceDao().deleteById(id));
    }

    public LiveData<List<DailyAttendanceRecord>> getDailyAttendanceRecordsByDateRange(String employeeId, Date startDate, Date endDate) {
        return database.dailyAttendanceDao().getDailyAttendanceRecordsByDateRange(employeeId, startDate, endDate);
    }

    public List<DailyAttendanceRecord> getDailyAttendanceRecordsByDateRangeSync(String employeeId, Date startDate, Date endDate) {
        return database.dailyAttendanceDao().getDailyAttendanceRecordsByDateRangeSync(employeeId, startDate, endDate);
    }

    public DailyAttendanceRecord getDailyAttendanceRecordByDate(String employeeId, Date date) {
        return database.dailyAttendanceDao().getDailyAttendanceRecordByDate(employeeId, date);
    }
}